package com.lhy.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lhy.user.entity.User;
import com.lhy.user.service.IUserService;
import com.lhy.user.vo.LoginVo;
import com.lhy.user.vo.RegisterVo;
import com.lhy.user.vo.RuleFormVo;
import com.lhy.user.vo.UserModifyVo;
import com.lhy.vaccine_common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/users")
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 用户登录
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        R r = userService.login(loginVo);
        return r;
    }

    /**
     * 注册方法
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        R r = userService.register(registerVo);
        return r;
    }

    /**
     * 获取疫苗详情
     * @return
     */
    @GetMapping("/userGetVaccine")
    public R userGetVaccine(){
        R r = userService.userGetVaccine();
        return r;
    }

    /**
     * 用户界面进行下单
     * @return
     */
    @PostMapping("/order")
    public R order(@RequestBody RuleFormVo ruleFormVo){
        R r = userService.order(ruleFormVo);
        return r;
    }

    @PostMapping("/userModify")
    public R userModify(@RequestBody UserModifyVo userModifyVo){
        User user = new User();

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("u_phone",userModifyVo.getPhone());

        User one = userService.getOne(userQueryWrapper);
        if (one != null){
            user.setId(Integer.valueOf(userModifyVo.getId()));
            user.setName(userModifyVo.getName());
            user.setPhone(userModifyVo.getPhone());
            user.setGender(userModifyVo.getGender());
            user.setTelephone(userModifyVo.getTelephone());
            user.setProvince(one.getProvince());
            user.setCity(one.getCity());
            user.setRegion(one.getRegion());

            UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
            userUpdateWrapper.eq("u_id",user.getId());
            boolean b = userService.update(user, userUpdateWrapper);
            if (b){
                return R.ok().put("data","200");
            }
        }

        return R.error();
    }
}
